package com.example.demo.Utils;

/**
 * Created by wangyg6 on 2020/8/4 17:00.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.DateCodec;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.Date;
import java.util.List;

/**
 * json转换工具类,使用fastjson
 * @author lx
 *
 */
public class JSONUtil {
	private static SerializerFeature[] serializerFeatures;

	static {
		//修改日期解析方式
		ParserConfig.getGlobalInstance().putDeserializer(Date.class, DateCodec.instance);
		SerializeConfig.getGlobalInstance().put(Date.class, DateCodec.instance);
		//输出格式
		serializerFeatures = new SerializerFeature[] { SerializerFeature.BrowserSecure,
				SerializerFeature.DisableCircularReferenceDetect };
	}

	public static SerializerFeature[] getSerializerFeatures() {
		return serializerFeatures;
	}

	public static JSONObject parseObject(String text) {
		return JSON.parseObject(text);
	}

	public static <T> T parseObject(String text, Class<T> clazz) {
		return JSON.parseObject(text, clazz);
	}

	public static <T> T parseObject(String text, TypeReference<T> type) {
		return JSON.parseObject(text, type);
	}

	public static <T> List<T> parseArray(String text, Class<T> clazz) {
		return JSON.parseArray(text, clazz);
	}

	public static String toJSONString(Object object) {
		return JSON.toJSONString(object, getSerializerFeatures());
	}

}
